package com.gxuwz.endrskyweather.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;

import com.google.android.material.button.MaterialButton;
import com.gxuwz.endrskyweather.viewmodel.FavoriteViewModel;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gxuwz.endrskyweather.R;
import com.gxuwz.endrskyweather.model.City;

import java.util.ArrayList;
import java.util.List;

public class AddCityListAdapter extends RecyclerView.Adapter<AddCityListAdapter.CityViewHolder> {
    private List<City> cities;

    public AddCityListAdapter(List<City> cities) {
        this.cities = cities;
    }

    @NonNull
    @Override
    public CityViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_city, parent, false);
        return new CityViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CityViewHolder holder, int position) {
        City city = cities.get(position);
        holder.bind(city);
    }

    @Override
    public int getItemCount() {
        return cities.size();
    }

    public void updateData(List<City> newCities) {
        if (cities == null) {
            cities = new ArrayList<>();
        }
        cities.clear();
        cities.addAll(newCities);
        notifyDataSetChanged();
        Log.d("CityAdapter", "Updated data with " + newCities.size() + " cities");
    }


    static class CityViewHolder extends RecyclerView.ViewHolder {
        TextView tvCityName;
        TextView tvProvince;
        TextView tvCityCode;
        MaterialButton btnAddCity;

        public CityViewHolder(@NonNull View itemView) {
            super(itemView);
            tvCityName = itemView.findViewById(R.id.tv_city_name);
            tvProvince = itemView.findViewById(R.id.tv_province);
            tvCityCode = itemView.findViewById(R.id.tv_city_code);
            btnAddCity = itemView.findViewById(R.id.btn_add_city);
        }

        public void bind(City city) {
            tvCityName.setText(city.getAreaCn());
            tvProvince.setText(city.getProvCn());
            tvCityCode.setText(city.getAreaCode());

            btnAddCity.setOnClickListener(v -> {
                FavoriteViewModel viewModel = new ViewModelProvider(
                        (ViewModelStoreOwner) v.getContext(),
                        ViewModelProvider.AndroidViewModelFactory.getInstance(
                                ((Activity) v.getContext()).getApplication()
                        )
                ).get(FavoriteViewModel.class);
                viewModel.addFavoriteCity(city.getAreaCode(), city.getAreaCn());
                // 获取添加结果
                viewModel.getAddFavoriteResult().observe((LifecycleOwner) v.getContext(), result -> {
                    if (result == 1) {
                        // 想要收藏的城市已经存在
                        Toast.makeText(v.getContext(), "该城市已在收藏列表中", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (result == -1) {
                        // 收藏失败
                        Toast.makeText(v.getContext(), "发生错误，收藏失败", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (result == 0) {
                        // 收藏成功
                        Toast.makeText(v.getContext(), "收藏成功", Toast.LENGTH_SHORT).show();
                    }
                });
            });
        }

    }
}
